package com.zhiqianbao.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.zhiqianbao.R;
import com.zhiqianbao.entity.AudioState;
import com.zhiqianbao.shared.SharedPreferanceUtils;
import com.zhiqianbao.utils.Constants;
import com.zhiqianbao.utils.HttpUtils;
import com.zhiqianbao.view.ProcessProgressBarView;

import java.util.HashMap;
import java.util.Map;

import cn.trinea.android.common.util.StringUtils;

import static com.zhiqianbao.shared.SharedPreferanceUtils.initSharedData;

/**
 * Created by 子龙 on 2017/4/17.
 * 认领成功
 */

public class CertificationSdActivity extends ApplyTabHostActivity implements View.OnClickListener{
    public static final int DATA_FAILED=0x123;
    public static final int DATA_SCUESSFUL=0x124;
    private Button button_firstIDAuthenActivity;
    private Button button_return;
    private TextView topbar_textview_title;
    private ProcessProgressBarView processView;
    private String job_state;
    private String identity_state;
    private String telecom_state;
    private String emergency_contact_state;
    private  SharedPreferanceUtils sharedPreferanceUtils;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activuty_certifysesussd);
        initView();
        addListner();
        initData();
        initSharedData(this);
    }

    private void initData() {
        sharedPreferanceUtils = new SharedPreferanceUtils(this);
        String userId = sharedPreferanceUtils.getUserId();
        String token_process = sharedPreferanceUtils.getToken();

        new Thread(new Runnable() {
            @Override
            public void run() {
                Message message = new Message();
                Map<String, String> mapParams = new HashMap<>();
                mapParams.put("userId", Constants.LOGIN_USER != null ? Constants.LOGIN_USER.getId() + "" : "");
                mapParams.put("token", Constants.LOGIN_USER != null ? Constants.LOGIN_USER.getToken() : "");
                String result = HttpUtils.httpPostString(Constants.API_ROOT + "/user/auth/authState",
                        mapParams, CertificationSdActivity.this);
                if (StringUtils.isBlank(result)) {
                    message.what = DATA_FAILED;
                } else {
                    message.what = DATA_SCUESSFUL;
                    AudioState data =gson.fromJson(result, AudioState.class);
                    message.obj = data;
                }
                postHandler.sendMessage(message);
            }
        }).start();
    }

    public void initView() {
        button_firstIDAuthenActivity = (Button) findViewById(R.id.button_firstIDAuthenActivity);
        button_return = (Button) findViewById(R.id.button_return);
        topbar_textview_title = (TextView) findViewById(R.id.topbar_textview_title);
        topbar_textview_title.setText("首次信息确认");
        processView= (ProcessProgressBarView) findViewById(R.id.process_layout);
        //芝麻开启状态,1开启，0未开启
        String zhimaOpenOrClose = sharedPreferanceUtils.getZhimaOpenOrClose();
        if("1".equals(zhimaOpenOrClose)){
            processView.openZhiMaProcess(ProcessProgressBarView.IS_SHOW_ZHIMA,5);
        }
        else{
            processView.openZhiMaProcess(ProcessProgressBarView.NOTIS_SHOW_ZHIMA,4);
        }
    }

    private void addListner() {
        button_firstIDAuthenActivity.setOnClickListener(this);
        button_return.setOnClickListener(this);
    }



    @Override
    public void onClick(View v) {
        if(R.id.button_firstIDAuthenActivity==v.getId()) {
            //跳到主页面
            SharedPreferanceUtils.setJumpInfo(true, 1, 1);
            Intent intent = new Intent(CertificationSdActivity.this, MainActivity.class);
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            intent.putExtra("tabPosition", 1);
            startActivity(intent);
            finish();
        }
        else if(R.id.button_return==v.getId()){
                finish();
        }
    }

    Handler postHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case DATA_FAILED:
                    showShortToast1(Constants.HTTP_ERROR_MESSAGE);
                    break;
                case DATA_SCUESSFUL:
                    AudioState response = (AudioState) msg.obj;
                    if (response == null){
                        showShortToast1("网络超时，请再试!");
                    }
                    if (response!=null){
                        if (response.getCode().equals("0")){
//                        closeWaitDialog();
                            emergency_contact_state = response.getData().getEmergency_contact_state();
                            identity_state = response.getData().getIdentity_state();
                            job_state = response.getData().getJob_state();
                            telecom_state = response.getData().getTelecom_state();
                            sharedPreferanceUtils.setTelecon(telecom_state);
                            sharedPreferanceUtils.seteEmergency(emergency_contact_state);
                            sharedPreferanceUtils.setJobState(job_state);
                            sharedPreferanceUtils.setidCardState(identity_state);

                        }
                    }
                    break;
            }

        }
    };
}
